<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class BasicConfig extends Basic
{
    
    public static function tableName()
    {
        return '{{%basic_config}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['lang', 'required', 'message' => 'lang不能为空', 'on' => ['add']],
            ['lang', 'string', 'message' => 'lang必须是字符串',
                     'max' => 8, 'tooLong' => 'lang最长8位'],
            ['website_name', 'required', 'message' => 'website_name不能为空'],
            ['website_name', 'string', 'message' => 'website_name必须是字符串',
                                'min' => 2, 'tooShort' => 'website_name最短2位',
                                'max' => 16, 'tooLong' => 'website_name最长16位'],

            ['seo_title', 'default', 'value' => ''],
            ['seo_title', 'string', 'message' => 'seo_title必须是字符串',
                                'min' => 2, 'tooShort' => 'seo_title最短2位',
                                'max' => 32, 'tooLong' => 'seo_title最长32位'],
            ['seo_keyword', 'default', 'value' => ''],
            ['seo_keyword', 'string', 'message' => 'seo_keyword必须是字符串',
                                'min' => 2, 'tooShort' => 'seo_keyword最短2位',
                                'max' => 128, 'tooLong' => 'seo_keyword最长128位'],
            ['seo_describe', 'default', 'value' => ''],
            ['seo_describe', 'string', 'message' => 'seo_describe必须是字符串',
                                'min' => 2, 'tooShort' => 'seo_describe最短2位',
                                'max' => 512, 'tooLong' => 'seo_describe最长512位'],
            ['copyright', 'default', 'value' => ''],
            ['copyright', 'string', 'message' => 'copyright必须是字符串',
                                'min' => 2, 'tooShort' => 'copyright最短2位',
                                'max' => 512, 'tooLong' => 'copyright最长512位'],
            ['lastmodify_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['lastmodify_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastmodify_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addConfig($data)
    {
        $data['BasicConfig']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyConfig($data, $app_id, $lang = 'zh')
    {
        $basicConfig = self::find()->where('app_id = :id and lang = :lang', [':id' => $app_id, ':lang' => $lang])->one();
        /*如果不存在，则需要添加一条*/
        if(empty($basicConfig)){
            $this->scenario = 'add';
            $basicConfig = new BasicConfig;
            $data['BasicConfig']['app_id'] = $app_id;
            // P($data);
            if($basicConfig->addConfig($data)){
                return true;
            }else{
                if($basicConfig->hasErrors()){
                    return $basicConfig->getErrors();
                }
            }
            return false;
        }
        if(!is_null($basicConfig)){
            if($this->load($data) and $this->validate()){
                if(!empty($this->website_name)){
                    $basicConfig->website_name = $this->website_name;
                }
                if(!empty($this->seo_title)){
                    $basicConfig->seo_title = $this->seo_title;
                }
                if(!empty($this->seo_keyword)){
                    $basicConfig->seo_keyword = $this->seo_keyword;
                }
                if(!empty($this->seo_describe)){
                    $basicConfig->seo_describe = $this->seo_describe;
                }
                if(!empty($this->copyright)){
                    $basicConfig->copyright = $this->copyright;
                }
                $basicConfig->lastmodify_time = date('Y-m-d H:i:s', time());
                // P($basicConfig);
                if($basicConfig->save(false)){
                    return true;
                }
            }else{
                if($this->hasErrors()){
                    return $this->getErrors();
                }
            }
        };
        return false;
    }

    
}